package org.ldh.dao.impl;

import org.ldh.dao.BookBorrowDao;
import org.ldh.domain.BookBorrow;
import org.ldh.util.ConnectionFactory;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 图书借阅的持久层实现类
 */
public class BookBorrowDaoImpl implements BookBorrowDao {
    @Override
    public List<BookBorrow> findAll() {
        //获取数据库连接
        Connection conn = ConnectionFactory.getConnection();
        PreparedStatement pstm = null;
        ResultSet rs = null;
        try{
            //编写sql
            String sql = "select * from bookBorrow";
            //获取执行sql的对象
            pstm = conn.prepareStatement(sql);
            //执行sql
            rs = pstm.executeQuery();
            List<BookBorrow> bookBorrows = new ArrayList<BookBorrow>();
            //指针默认指向表头，向下移动一行
            while(rs.next()){
                //从数据库中查询出数据，封装成实体类
                BookBorrow bookBorrow = new BookBorrow();
                bookBorrow.setBookId(rs.getString("book_id"));
                bookBorrow.setBookName(rs.getString("book_name"));
                bookBorrow.setBorrowTime(rs.getString("borrow_time"));
                bookBorrow.setReturnTime(rs.getString("return_time"));

                bookBorrows.add(bookBorrow);
            }
            return bookBorrows;
        }catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnectionFactory.close(conn,pstm,rs);
        }
        return null;
    }

    @Override
    public BookBorrow findById(String bookId) {
        //获取数据库连接
        Connection conn = ConnectionFactory.getConnection();
        PreparedStatement pstm = null;
        ResultSet rs = null;
        try{
            //编写sql
            String sql = "select * from bookBorrow where book_id = ?";
            //获取执行sql的对象
            pstm = conn.prepareStatement(sql);
            //向占位符传参
            pstm.setString(1,bookId);
            //执行sql
            rs = pstm.executeQuery();
            //指针默认指向表头，向下移动一行
            while(rs.next()){
                //从数据库中查询出数据，封装成实体类
                BookBorrow bookBorrow = new BookBorrow();
                bookBorrow.setBookId(rs.getString("book_id"));
                bookBorrow.setBookName(rs.getString("book_name"));
                bookBorrow.setBorrowTime(rs.getString("borrow_time"));
                bookBorrow.setReturnTime(rs.getString("return_time"));

                return bookBorrow;
            }
        }catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnectionFactory.close(conn,pstm,rs);
        }
        return null;
    }

    @Override
    public void save(BookBorrow bookBorrow) {
        //获取数据库连接
        Connection conn = ConnectionFactory.getConnection();
        PreparedStatement pstm = null;
        try{
            //编写sql
            String sql = "insert into bookBorrow(book_id,book_name,borrow_time,return_time) values(?,?,?,?)";
            //获取执行sql的对象
            pstm = conn.prepareStatement(sql);
            //向占位符传参
            pstm.setString(1,bookBorrow.getBookId());
            pstm.setString(2,bookBorrow.getBookName());
            pstm.setString(3, bookBorrow.getBorrowTime());
            pstm.setString(4, bookBorrow.getReturnTime());
            //执行sql
            pstm.executeUpdate();

        }catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnectionFactory.close(conn,pstm);
        }
    }

    @Override
    public void update(BookBorrow bookBorrow) {
        //获取数据库连接
        Connection conn = ConnectionFactory.getConnection();
        PreparedStatement pstm = null;
        try{
            //编写sql
            String sql = "update bookBorrow set book_name = ?,borrow_time = ?,return_time = ? where book_id = ?";
            //获取执行sql的对象
            pstm = conn.prepareStatement(sql);
            //向占位符传参
            pstm.setString(1,bookBorrow.getBookName());
            pstm.setString(2, bookBorrow.getBorrowTime());
            pstm.setString(3, bookBorrow.getReturnTime());
            pstm.setString(4, bookBorrow.getBookId());
            //执行sql
            pstm.executeUpdate();

        }catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnectionFactory.close(conn,pstm);
        }
    }

    @Override
    public void delete(String bookId) {
        //获取数据库连接
        Connection conn = ConnectionFactory.getConnection();
        PreparedStatement pstm = null;
        try{
            //编写sql
            String sql = "delete from bookBorrow where book_id = ?";
            //获取执行sql的对象
            pstm = conn.prepareStatement(sql);
            //向占位符传参
            pstm.setString(1, bookId);
            //执行sql
            pstm.executeUpdate();

        }catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnectionFactory.close(conn,pstm);
        }
    }
}
